﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using aspZone.Commerce.Model;

namespace aspZone.Commerce.Web.App.Models.Shared
{
    public class CategoryMenuItem
    {
        private int currentCategoryId = 0;

        public CategoryMenuItem(Category category)
        {
            this.Id = category.Id;
            this.Name = category.Name;
            this.UrlName = category.UrlName;
            this.ShowChildren = false;
            this.RenderLink = (category.Id != currentCategoryId);

            this.Children = new List<CategoryMenuItem>();
            foreach (var child in category.ChildCategories)
            {
                if (child.DisplayInCategoryMenu)
                {
                    var childItem = new CategoryMenuItem(child);
                    this.Children.Add(childItem);
                    if (childItem.ShowChildren)
                    {
                        this.ShowChildren = true;
                    }
                }
            }
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string UrlName { get; set; }
        public bool ShowChildren { get; set; }
        public bool RenderLink { get; set; }
        public IList<CategoryMenuItem> Children { get; set; }

        public int CurrentCategoryId
        {
            get
            {
                return this.currentCategoryId;
            }
            set
            {
                this.currentCategoryId = value;
                this.ShowChildren = (this.Id == this.currentCategoryId);
                foreach (var child in this.Children)
                {
                    child.CurrentCategoryId = value;
                    if (child.ShowChildren)
                    {
                        this.ShowChildren = true;
                    }
                }
            }
        }
    }
}
